package com.zzk.lanouhn.myapplication1.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.AuthorCollectSingleGoodActivity;
import com.zzk.lanouhn.myapplication1.adapters.AuthorsCollectArticlesAdapter;
import com.zzk.lanouhn.myapplication1.adapters.MyGridViewSingleGoodAdapter;
import com.zzk.lanouhn.myapplication1.modles.CollectArticles;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;
import com.zzk.lanouhn.myapplication1.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者信息->收藏
 * Created by lanouhn on 16/9/10.
 */
public class AuthorsCollectFragment extends Fragment {

    private RecyclerView xrecyclerView;
    private MyGridView gridview;
    private AuthorsCollectArticlesAdapter adapter;
    private MyGridViewSingleGoodAdapter gridviewAdapter;
    private List<CollectArticles.DataBean.TopicBean> collects = new ArrayList<>();
    private List<CollectArticles.DataBean.ProductBean> singlegood = new ArrayList<>();
    private SharedPreferences preferences;
    private String authorId;
    private RelativeLayout rel_article,rel_singlegood,rel_bg;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.authorinfo_collect_fragment, null);
        xrecyclerView = (RecyclerView) view.findViewById(R.id.recy_collect_articles);
        gridview= (MyGridView) view.findViewById(R.id.gv_single_good);
        rel_article= (RelativeLayout) view.findViewById(R.id.rel_article);
        rel_singlegood= (RelativeLayout) view.findViewById(R.id.rel_singlegood);
        rel_bg= (RelativeLayout) view.findViewById(R.id.rel_bg);
        initData();
        initEvent();
        getData();
        return view;
    }

    private void initData() {
        preferences = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        authorId = preferences.getString("authorId", "default");
    }

    private void getData() {
        String url = " http://open4.bantangapp.com/users/center/collect?";
        String dataUrl = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7&os_versions=5.0.2&screensize=720&v=19" +
                "&visit_user_id="+authorId+"&page=0&pagesize=20";
        try {
            HttpUtils.doPostAsyn(url, dataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    collects = gson.fromJson(result, CollectArticles.class).getData().getTopic();
                    if (collects.size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(3);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            HttpUtils.doPostAsyn(url, dataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson1=new Gson();
                    singlegood=gson1.fromJson(result,CollectArticles.class).getData().getProduct();
                    if (singlegood.size() > 0) {
                        handler.sendEmptyMessage(2);
                    } else {
                        handler.sendEmptyMessage(4);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
//        if (collects.size()==0&&singlegood.size()==0) {
//            handler.sendEmptyMessage(0);
//        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    rel_bg.setVisibility(View.VISIBLE);
                    break;
                case 1:

                    adapter = new AuthorsCollectArticlesAdapter(getActivity(), collects);
                    xrecyclerView.setAdapter(adapter);
                    LinearLayoutManager manager = new LinearLayoutManager(getActivity());
                    manager.setOrientation(LinearLayoutManager.VERTICAL);
                    xrecyclerView.setLayoutManager(manager);
                    break;
                case 2:
                    gridviewAdapter=new MyGridViewSingleGoodAdapter(getActivity(),singlegood);
                    gridview.setAdapter(gridviewAdapter);
                    break;
                case 3:
                    rel_article.setVisibility(View.INVISIBLE);
                    xrecyclerView.setVisibility(View.INVISIBLE);
                    break;
                case 4:
                    rel_singlegood.setVisibility(View.INVISIBLE);
                    gridview.setVisibility(View.INVISIBLE);
                    break;
                default:

                    break;
            }
        }
    };
    private void initEvent(){
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(),AuthorCollectSingleGoodActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("authorSingle",singlegood.get(position).getId());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
//        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent=new Intent(getActivity(), AuthorCollectSingleGoodActivity.class);
//                Bundle bundle=new Bundle();
//                bundle.putString("authorSingle",singlegood.get(position).getId());
//                intent.putExtras(bundle);
//                startActivity(intent);
//            }
//        });
    }
}
